<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/27
 * 描述:
 */

namespace app\result\admin;

use app\model\chat\ChatConversationsSummary;
use app\model\Feedback;
use app\model\goods\Goods;
use app\model\KeFu;
use app\model\order\Order;
use app\model\SysStatistics;
use app\model\user\User;
use app\result\BaseResult;

class StatisticsResult extends BaseResult
{
//后台数据统计
    public function getAdminStatistic($args = []): array
    {
        $toDoList = [
            ['label' => '待处理反馈', 'color' => 'green', 'unit' => '条',
                'route_name' => 'feedbackIndex',
                'value' => (new Feedback())->data_count(['status' => 0]),
            ],
            ['label' => '待发货订单', 'color' => 'red', 'unit' => '单',
                'route_name' => 'need_send',
                'value' => (new  Order())->data_count([['order_status', '=', 1], ['pay_status', '=', 1]]),
            ],
            ['label' => '待审核订单', 'color' => 'blue', 'unit' => '单',
                'route_name' => 'need_check',
                'value' => (new  Order())->data_count([['order_status', '=', 7]]),
            ],
            ['label' => '售后待处理', 'color' => 'orange', 'unit' => '单',
                'route_name' => 'refund',
                'value' => (new  Order())->data_count([['order_status', '=', 5]]),
            ],

            ['label' => '待付款订单', 'color' => 'pink', 'unit' => '单',
                'route_name' => 'need_pay',
                'value' => (new  Order())->data_count([['order_status', '=', 0], ['pay_status', '=', 0]]),
            ],
            ['label' => '待收货订单', 'color' => 'gray', 'unit' => '单',
                'route_name' => 'need_receive',
                'value' => (new  Order())->data_count([['order_status', '=', 2]]),
            ],
            ['label' => '客服待回复', 'color' => 'purple', 'unit' => '条',
                'route_name' => 'kefuIndex',
                'value' => (new  ChatConversationsSummary())->data_sum([
                    ['read_time', '=', 0],
                    ['is_deleted', '=', 0],
                    ['user_id', 'in', (new KeFu())->where('status', '=', 1)->column('kefu_id')],
                ], 'unread_count'),
            ],
            ['label' => '库存不足商品', 'color' => 'red', 'unit' => '个',
                'route_name' => 'goodsIndex',
                'value' => (new Goods())->data_count([['store_count', '<=', 10], ['del_time', '=', 0], ['is_on_sale', '=', 1]]),
            ],
            ['label' => '用户数量', 'color' => 'blue', 'unit' => '个',
                'route_name' => 'user',
                'value' => (new  User())->data_count([]),
            ],
        ];
        $charts = [
            ['option' => $this->sys_statistics_data(), 'name' => '系统统计信息'],
        ];


        return self::success_return('success', [
            'charts' => $charts,
            'todoList' => $toDoList
        ]);

    }

    private function sys_statistics_data()
    {
        $model = new SysStatistics();
        $startDate = date('Y-m-d', strtotime("-1 month"));
        $data = $model->data_list_nopage([['date', '>=', $startDate]], '*', 'date asc');
        $legendData = [
            ['name' => '注册用户量(个)', 'field' => 'reg_user_count'],
            ['name' => '活跃用户量(个)', 'field' => 'active_user_count'],
            ['name' => '订单量(笔)', 'field' => 'order_count'],
            ['name' => '订单总金额(元)', 'field' => 'order_amount'],
            ['name' => '实付金额(元)', 'field' => 'order_amount_paid'],
            ['name' => '下单用户数(个)', 'field' => 'order_user_count'],
            ['name' => '接口访问次数（次）', 'field' => 'api_count'],
        ];
        $series = [];
        $category = array_column($data, 'date');

        foreach ($legendData as $item) {
            $series[] = [
                'data' => array_column($data, $item['field']),
                'name' => $item['name'],
                'type' => 'line',
                'smooth' => true
            ];
        }

        //按月统计
        $dataFormat = [
            'title' => [
                'text' => '系统统计信息',
                'subtext' => ''
            ],
            'tooltip' => [
                'trigger' => 'axis'
            ],
            'legend' => [
                'data' => $legendData
            ],
            'xAxis' => [
                'type' => 'category',
                'data' => $category
            ],
            'yAxis' => [
                'type' => 'value',
            ],
            'series' => $series
        ];

        return $dataFormat;

    }
}
